﻿using System;
using CoreSoft.Frame.Entity.http;
using U8Login;
using UFIDA.U8.MomServiceCommon;

namespace FERROU8API.Plugin
{
    public class MomaterielsPlugin
    {
        //private readonly string _ip = ConfigurationManager.AppSettings["WmsIP"] + "Moandommaterielsapi";
        public static string constr = "";
        private string url = "";

        private String GetCustJs(clsLogin login, MSXML2.IXMLDOMDocument2 domhead, int operation)
        {
            //MSXML2.IXMLDOMNode tmpNode = domhead.selectSingleNode("/customer");
            //String custcode = tmpNode.attributes.getNamedItem("ccuscode").nodeValue.ObjToString();
            //String custname = tmpNode.attributes.getNamedItem("ccusname").nodeValue.ObjToString();
            //string ccode = tmpNode.attributes.getNamedItem("ccccode").nodeValue.ObjToString();
            //MSXML2.IXMLDOMNode tmphead = domhead.selectSingleNode("//z:row");

            //string allocateid = domhead.selectSingleNode("//allocateid").nodeTypedValue.ObjToString();//生产订单的所有子件用料资料ID 
            //int modid = domhead.selectSingleNode("//modid").nodeTypedValue.ObjToInt32();//生产订单明细ID  
            //DateTime modate = tmphead.attributes.getNamedItem("modate").nodeValue.ObjToDate();//单据时间
            //string deptCode = tmphead.attributes.getNamedItem("cdepcode").nodeValue.ObjToString();//部门编码
            //string userCode = tmphead.attributes.getNamedItem("cpersonCode").nodeValue.ObjToString();//人员编码
            //string bz = tmphead.attributes.getNamedItem("cexch_name").nodeValue.ObjToString();//币种 cexch_name 
            //float exchangeRate = tmphead.attributes.getNamedItem("iexchrate").nodeValue.ObjToFloat();//汇率
            //float taxRate = tmphead.attributes.getNamedItem("itaxrate").nodeValue.ObjToFloat();//税率 iTaxRate
            ////string xxx = dombody

            //MSXML2.IXMLDOMNodeList tmpbody = dombody.selectNodes("//z:row");

            //List<Moandommaterielsapi> dlist = new List<Moandommaterielsapi>();

            //foreach (MSXML2.IXMLDOMNode item in tmpbody)
            //{
            //    int disSeq = item.attributes.getNamedItem("irowno").nodeValue.ObjToString();//行号
            //    string sourceDid = item.attributes.getNamedItem("autoid").nodeValue.ObjToString();//来源明细id
            //    string soCode = item.attributes.getNamedItem("autoid").nodeValue.ObjToString();//销售订单号
            //    Int32 StartFlag = 1;
            //    Moandommaterielsapi m = new Moandommaterielsapi();
            //    m.OmFlag = 0;
            //    dlist.Add(m);
            //}

            //ApiRequest req = new ApiRequest();
            //req.data = dlist.ToJson();
            //req.orgcode = login.get_cAcc_Id();
            //req.primarykey = mocode;
            //req.operation = operation;
            //return req.ToJson();
            return "";
        }
        public bool Update_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 2);

                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Insert_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 0);

                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
        public bool Delete_After(ref MSXML2.IXMLDOMDocument2 domhead, MSXML2.IXMLDOMDocument2 dombody, ref string errMsg)
        {
            //获取插件上下文
            bool bresult = true;
            MomCallContext ctx = MomCallContextCache.Instance.CurrentMomCallContext;

            //从上下文获取U8Login对象
            U8Login.clsLogin login = ctx.U8Login as U8Login.clsLogin;
            if (login != null)
                constr = login.UFDataConnstringForNet;
            String js;
            try
            {
                js = GetCustJs(login, domhead, 1);

                var result = HttpHelper.PostDataToServer(url, js);
                var apiresult = Common.DeserializeObject<ApiResponse>(result);
                if (apiresult.successful != 0)
                {
                    errMsg = apiresult.message;
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                bresult = false;
                errMsg = ex.Message;
            }
            return bresult;
        }
    }
}